package com.gzsxy.esjy.service.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzsxy.esjy.common.base.exception.impl.CustomAssert;
import com.gzsxy.esjy.common.base.result.ResultCodeEnum;
import com.gzsxy.esjy.service.base.dto.request.AttributeRequest;
import com.gzsxy.esjy.service.base.dto.response.AttributeRsp;
import com.gzsxy.esjy.service.base.util.ValidateUtil;
import com.gzsxy.esjy.service.product.dao.AttributeMapper;
import com.gzsxy.esjy.service.product.entity.Attribute;
import com.gzsxy.esjy.service.product.entity.vo.AttributeQueryVo;
import com.gzsxy.esjy.service.product.service.AttributeService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author xiaolong
 * @version 1.0
 * @description: 商品参数
 * @date 2022/1/10 19:47
 */
@Service
public class AttributeServiceImpl extends ServiceImpl<AttributeMapper, Attribute> implements AttributeService {


    @Autowired
    private AttributeMapper attributeMapper;


    /**
     * @description: 根据三级分类id查询对应的参数
     * @param catId 分类id
     * @param attrSel 参数类型
     * @return
     */
    @Override
    public List<AttributeRsp> getAttributeByCategoryId(String catId,String attrSel) {

        LambdaQueryWrapper<Attribute> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Attribute::getCatId,catId);
        wrapper.eq(Attribute::getAttrSel,attrSel);
        List<Attribute> attributes = attributeMapper.selectList(wrapper);
        //转为统一AttributeRsp返回值
        List<AttributeRsp> attributeRsp = JSON.parseArray(JSON.toJSONString(attributes), AttributeRsp.class);
        List<String> a = Arrays.asList("");
        //赋空值，防止前端为null对象
        attributeRsp.stream().map(attributeRsp1 ->  {attributeRsp1.setCheckedCities(a);return attributeRsp1;}).collect(Collectors.toList());
        return attributeRsp;
    }




    /**
     * @description: 添加参数
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addAttribute(AttributeRequest attributeRequest) {
        //校验唯一参数
        ValidateUtil.assertUnique(attributeMapper::existByAttributeName,attributeRequest.getAttrName(),attributeRequest.getCatId(),ResultCodeEnum.PRODUCT_ATTRIBUTE_EXIST_ERROR.getMessage());
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeRequest,attribute);
        attributeMapper.insert(attribute);
    }


    /**
     * @description: 修改参数
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateAttribute(AttributeRequest attributeRequest) {
        Attribute attribute = attributeMapper.selectById(attributeRequest.getId());
        //商品参数不存在
        CustomAssert.notTrue(ObjectUtils.isNotEmpty(attribute), ResultCodeEnum.PRODUCT_ATTRIBUTE_NOT_FOUND.getMessage(),ResultCodeEnum.PRODUCT_ATTRIBUTE_NOT_FOUND.getCode());
        Attribute attribute1 = new Attribute();
        BeanUtils.copyProperties(attributeRequest,attribute1);
        //修改了商品参数
        if (!attribute.getAttrName().equals(attributeRequest.getAttrName())){
            //校验唯一参数
            ValidateUtil.assertUnique(attributeMapper::existByAttributeName,attributeRequest.getAttrName(),attributeRequest.getCatId(),ResultCodeEnum.PRODUCT_ATTRIBUTE_EXIST_ERROR.getMessage());
        }
        attributeMapper.updateById(attribute1);
    }


    /**
     * @description: 删除参数
     * @param id 参数id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteAttribute(String id) {
        Attribute attribute = attributeMapper.selectById(id);
        //商品参数不存在
        CustomAssert.notTrue(ObjectUtils.isNotEmpty(attribute),ResultCodeEnum.PRODUCT_ATTRIBUTE_NOT_FOUND.getMessage(),ResultCodeEnum.PRODUCT_ATTRIBUTE_NOT_FOUND.getCode());
        attributeMapper.deleteById(id);
    }


    /**
     * @description: 根据查询条件查询参数
     * @param
     * @return
     */
    @Override
    public AttributeRsp getAttributeById(AttributeQueryVo attributeQueryVo) {
        String id = attributeQueryVo.getId();
        String catId = attributeQueryVo.getCatId();
        String attrSel = attributeQueryVo.getAttrSel();
        LambdaQueryWrapper<Attribute> wrapper = Wrappers.lambdaQuery();

        if (StringUtils.isNotEmpty(id)){
            wrapper.eq(Attribute::getId,id);
        }
        if (StringUtils.isNotEmpty(catId)){
            wrapper.eq(Attribute::getCatId,catId);
        }
        if (StringUtils.isNotEmpty(attrSel)){
            wrapper.eq(Attribute::getAttrSel,attrSel);
        }
        AttributeRsp attributeRsp = new AttributeRsp();
        Attribute attribute = attributeMapper.selectOne(wrapper);
        BeanUtils.copyProperties(attribute,attributeRsp);
        return attributeRsp;
    }
}
